<template>
  <div id="home">
    <HeaderTemplate/>
    <BodyTemplate
      :current-view="currentView"
    />
    <!-- <FooterTemplate /> -->
  </div>
</template>

<script>
  export default {
    name: 'home',
    data: function () {
      return {
        matchId: 0,
        currentWalletAddress: '',
        isNotifierShown: false
      }
    },
    props: ['currentView'],
    mounted () {
      // 查询用户信息
      this.$root.callMatch({
        params: null,
        methodName: API.GET_ACCOUNTS,
        methodType: 'GET',
        success: (data) => {
          this.currentWalletAddress = data

          this.$nextTick(function () {
            setInterval(this.timer, 8000)
          })
        },
        failure: null
      })
    },
    methods: {
      showMatchNotify () {
        this.isNotifierShown = true
        this.$notify.success({
          title: 'Notice',
          message: 'U have a new challenge!',
          showClose: true,
          duration: 0,
          offset: 100,
          onClick: () => {
            // ddddd
            this.$router.push({
              path: '/match',
              query: {
                id: this.matchId
              }
            })
          }
        })
      },
      timer () {
        this.$root.callMatch({
          isProtected: true,
          methodName: API.CONCERNED_CHALLENGES,
          methodType: 'GET',
          success: data => {
            var length = data.length
            if (length > 0) {
              this.getMatchDetail(parseInt(data[length - 1]))
            }
          },
          failure: null
        })
      },
      getMatchDetail (matchIdInt) {
        this.$root.callMatch({
          isProtected: false,
          params: matchIdInt,
          methodName: API.GET_MATCH_DETAIL,
          methodType: 'GET',
          success: data => {
            // console.log('GET_MATCH_DETAIL = ' + JSON.stringify(data))

            // address from, address to, int token_from, int token_to, int 赌注, int 阶段, int 比赛id
            // this.matchDetail = {
            //   from: data[0], // 如果是我 则 我挑战别人
            //   to: data[1], // 如果是我，则 别人挑战我
            //   tokenFrom: data[2],
            //   token_to: data[3],
            //   wager: data[4],
            //   status: data[5],
            //   id: data[6]
            // }
            console.log('status = ' + data[5] + ', data[1] = ' + data[1] + ', current = ' + this.walletAddress)
            if (data[5] === 1 && data[1] === this.currentWalletAddress) {
              // this.isMatchWaitingToAccept = true
              if (!this.isNotifierShown) {
                this.showMatchNotify()
              }
            }
          },
          failure: null
        })
      }
    },
    components: {
      HeaderTemplate,
      BodyTemplate,
      FooterTemplate
    }
  }

  import HeaderTemplate from './layout/HeaderTemplate'
  import BodyTemplate from './layout/BodyTemplate'
  import FooterTemplate from './layout/FooterTemplate'

  import { API } from '../util/constants'
</script>

<style scoped>
  #home {
    width: 100%;
  }
</style>
